<?php
/**
 * 聚合发送短信对象
 */
namespace logic\sys\message\mobile;
class MessageJh{
    
    public function send($mobile,$tplId,$tplValue){
        $messageValue = '';
        if( is_array($tplValue) ){
            //#code#=1234&#name#=中文名字
            foreach ($tplValue as $key => $value) {
                $messageValue = $messageValue.'#'.$key.'#='.$value.'&';
            }
            $messageValue = rtrim($messageValue,'&');
        }
		$sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
		$smsConf = array(
		    'key'       => 'd14e467e656c33cb1d6f13620d48fc1c1', //您申请的APPKEY
		    'mobile'    => $mobile, //接受短信的用户手机号码
		    'tpl_id'    => $tplId, //您申请的短信模板ID，根据实际情况修改
		    'tpl_value' => $messageValue, //您设置的模板变量，根据实际情况修改
		);
		$content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
        if( $content['error_code'] == 0 ){
            $result = array(
                'ret'    => true,
                'msg'    => $content['result']['sid'],
            );
        }else{
            $result = array(
                'ret'    => false,
                'msg'    => $content['reason'],
            );
        }
        return $result;
    }

    /**
	 * 请求接口返回内容
	 * @param  string $url [请求的URL地址]
	 * @param  string $params [请求的参数]
	 * @param  int $ipost [是否采用POST形式]
	 * @return  string
	 */
    private	function juhecurl($url,$params=false,$ispost=0){
	    $httpInfo = array();
	    $ch = curl_init();
	    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
	    curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
	    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
	    curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
	    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
	    if( $ispost )
	    {
	        curl_setopt( $ch , CURLOPT_POST , true );
	        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
	        curl_setopt( $ch , CURLOPT_URL , $url );
	    }
	    else
	    {
	        if($params){
	            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
	        }else{
	            curl_setopt( $ch , CURLOPT_URL , $url);
	        }
	    }
	    $response = curl_exec( $ch );        
	    if ($response === FALSE) {
            $result = array(
                'reason'    => 'cURL Error'.curl_error($ch),
                'result'    => false,
                'error_code'=> 1
            );
	    }else{
	       $result = json_decode($response,true);
	    }
        curl_close( $ch );
        return $result;
	 }
    
}